package com.yiwich.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.influxdb.InfluxDB;
import org.influxdb.dto.Point;
import org.influxdb.dto.QueryResult;
import org.influxdb.dto.QueryResult.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yiwich.dao.influx.InfluxDao;
import com.yiwich.model.mapped.Servermon;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value = "/influx")
@Slf4j
public class InfluxController {
	
	@Autowired
	InfluxDao influxDao;
	
	@GetMapping(value = "addTestData")
	@ResponseBody
    public Object insertOne() {
		Servermon servermon = new Servermon();
		servermon.setTime(System.currentTimeMillis()+"");
		servermon.setServerId("1");
		servermon.setCpuUse(20.0);
		servermon.setMemUse(20.0);
		servermon.setDiskUse(20.0);
		influxDao.insert(servermon);
        return "ok";
    }
	
	@GetMapping(value = "addTestDatas")
	@ResponseBody
    public Object insertList() {
		List<Servermon> serverList = new ArrayList<Servermon>();
		Servermon servermon1 = new Servermon();
		servermon1.setTime(System.currentTimeMillis()+"");
		servermon1.setServerId("1");
		servermon1.setCpuUse(20.0);
		servermon1.setMemUse(20.0);
		servermon1.setDiskUse(20.0);
		serverList.add(servermon1);
		Servermon servermon2 = new Servermon();
		servermon2.setTime(System.currentTimeMillis()+"");
		servermon2.setServerId("2");
		servermon2.setCpuUse(30.0);
		servermon2.setMemUse(30.0);
		servermon2.setDiskUse(30.0);
		serverList.add(servermon2);
		influxDao.insert(serverList);
        return "ok";
    }
	
	@GetMapping(value = "queryTestDatas")
	@ResponseBody
	public Object query() {
		List<Servermon> serverList = influxDao.query(Servermon.class, "select * from servermon");
		return serverList;
	}
	
	@GetMapping(value = "queryDatasByServer")
	@ResponseBody
	public Object queryByServer(String id) {
		List<Servermon> serverList = influxDao.query(Servermon.class, "select * from servermon where serverId='" + id + "'");
		return serverList;
	}
}
